package com.ljh.controller;

import com.ljh.pojo.Admin;
import com.ljh.service.IAdminService;
import com.ljh.utils.RespBean;
import com.ljh.vo.AdminLoginParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Api(tags = "LoginController")
@RestController
public class LoginController {
    @Autowired
    private IAdminService adminService;


    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){
        return adminService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),
                adminLoginParam.getCode(),request);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("注销成功");
    }


    //Principal principal获取当前登录对象
    @ApiOperation(value = "获取当前登录信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if (principal==null)
            return null;
        String name = principal.getName();
        Admin admin=adminService.getAdminByName(name);
        admin.setPassword(null);
        admin.setRoles(adminService.getRoleByAdminId(admin.getId()));
        return admin;
    }










    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    //有权限
    @GetMapping("/employee/basic/hello")
    public String hello1(){
        return "/employee/basic/hello";
    }

    //无权限
    @GetMapping("/employee/advanced/hello")
    public String hello2(){
        return "/employee/advanced/hello";
    }

    //有权限
    @GetMapping("/personnel/ec/hello")
    public String hello3(){
        return "/personnel/ec/**";
    }
}
